package com.sanery.ui.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sanery.entity.Record;
import com.sanery.service.BankService;

@Controller
public class BankController {

	@Resource(name = "bankService")
	private BankService service = null;

	@RequestMapping(value = "/getRecords", method = RequestMethod.POST)
	@ResponseBody
	public List<Record> getRecords(HttpSession session, Integer id, HttpServletResponse response) {
		List<Record> list = service.getRecords(id);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		return list;
	}

	@RequestMapping("/getCardNo")
	@ResponseBody
	public String getCardNo(Integer id, HttpServletResponse response) {
		String cardno = service.getCardNo(id);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		return cardno;
	}

	@RequestMapping("/disposit")
	@ResponseBody
	public String disposit(Float money, String cardno, Integer id, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		service.disposit(money, cardno, id);
		return "success";
	}

	@RequestMapping(value = "/getToAccount", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getToAccount(String toAccount, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		String name = service.getToAccount(toAccount);
		return name;
	}

	@RequestMapping(value = "/transfer")
	@ResponseBody
	public String transfer(Integer userId, String toAccount, Float money, String cardno, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		service.transfer(userId, toAccount, money, cardno);
		return "success";
	}

	@RequestMapping(value = "/getMoney")
	@ResponseBody
	public Float getMoney(Integer id, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Cache-Control", "no-cache");
		Float money = service.getMoney(id);
		return money;
	}
}
